package com.example.demo.service.impl;

import com.example.demo.config.MongoDBClient;
import com.example.demo.entity.Catalog;
import com.example.demo.entity.Menu;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.CodeEnum;
import com.example.demo.mapper.MenuMapper;
import com.example.demo.service.MenuService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.mongodb.core.query.Criteria;
//import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
public class MenuServiceImpl implements MenuService {


    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MongoDBClient mongoDBClient;

    @Override
    public Menu findById(Long id) {
        return menuMapper.findById(id);
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuMapper.findAllMenu();
    }


    /**
     * TODO: 把findAllMenu()和这个区分开
     * @return
     */
    @Override
    public List<Menu> queryMenuList(Integer page,Integer limit) {
        int index = (page-1)*limit;
        return menuMapper.queryMenuList(index,limit);
    }

    @Override
    public List<?> findCatalogById(Long id) {
        List<?> list = mongoDBClient.getAllByCollectionName(Catalog.class,"catalog");
        return list;
    }

    @Override
    public void CreateCollection(String collection) {
        mongoDBClient.createCollection(collection);
    }


    /**
     * @yucs 2021/8/29
     * 删除菜品
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteById(HttpSession session, Long id) throws BusinessException {
        return menuMapper.deleteById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    /**
     * TODO: 即使在这加了admin的权限限制，也不会触发授权；
     */
    @RequiresRoles({"admin"})
    public boolean offLoadById(HttpSession session, Long id) throws BusinessException {

        return menuMapper.offLoadById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @RequiresRoles({"admin"})
    public boolean upLoadById(HttpSession session, Long id) throws BusinessException {

        return menuMapper.upLoadById(id);
    }

    //更新用户
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer updateMenu(Menu menu){
        menuMapper.updateMenu(menu);
        return 1;
    }

    //删除用户
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer deleteMenu(Long id){
        menuMapper.deleteMenu(id);
        return 1;
    }

    //添加菜品
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Integer createMenu(Menu menu){
        menuMapper.createMenu(menu);
        return 1;
    }
}
